// 封装antv的图表导出Url
import JSZipUtils from "jszip-utils"
import docxtemplater from "docxtemplater"
import { saveAs } from 'file-saver'
import PizZip from 'pizzip';
export function toDataURL(chart) {
    const canvas = chart.getCanvas();
    const renderer = chart.renderer;
    const canvasDom = canvas.get('el');
    let dataURL = '';
    if (renderer === 'svg') {
        const clone = canvasDom.cloneNode(true);
        const svgDocType = document.implementation.createDocumentType(
            'svg',
            '-//W3C//DTD SVG 1.1//EN',
            'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
        );
        const svgDoc = document.implementation.createDocument('http://www.w3.org/2000/svg', 'svg', svgDocType);
        svgDoc.replaceChild(clone, svgDoc.documentElement);
        const svgData = new XMLSerializer().serializeToString(svgDoc);
        dataURL = 'data:image/svg+xml;charset=utf8,' + encodeURIComponent(svgData);
    } else if (renderer === 'canvas') {
        dataURL = canvasDom.toDataURL('image/png');
    }
    return dataURL;
}
export function getImg(graph,graphData) {
    graphData[i] = toDataURL(graph[i].chart)
}
//图片处理
export function base64DataURLToArrayBuffer(dataURL) {
    const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
    if (!base64Regex.test(dataURL)) {
        return false;
    }
    const stringBase64 = dataURL.replace(base64Regex, "");
    let binaryString;
    if (typeof window !== "undefined") {
        binaryString = window.atob(stringBase64);
    } else {
        binaryString = new Buffer(stringBase64, "base64").toString("binary");
    }
    const len = binaryString.length;
    const bytes = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        const ascii = binaryString.charCodeAt(i);
        bytes[i] = ascii;
    }
    return bytes.buffer;
}
//导出为word 高、宽、名称可指定
export function exportWord(height,width,name,module,data,dataName,graph = [],graphData = [],extraData = null,extraDataName = null) {
    if (graph.length) {
        this.getImg(graph,graphData);
        //这里要引入处理图片的插件，下载docxtemplater后，引入的就在其中了
        const ImageModule = require('docxtemplater-image-module-free');
    }
    const _this = this;
    //Word路径，在public文件下(CLI3)
    JSZipUtils.getBinaryContent(`${module}.docx`, function (error, content) {
        if (error) {
            throw error
        }
        let opts = {}
        opts.centered = true;
        opts.fileType = "docx";
        if (graph.length){
            opts.getImage = (tag) => {
                return _this.base64DataURLToArrayBuffer(tag);
            }
            opts.getSize = () => {
                // return [600, 400]//这里可更改输出的图片宽和高
                return [width,height]
            }
        }
        let zip = new PizZip(content);
        let doc = new docxtemplater();
        if (graph.length) {
            doc.attachModule(new ImageModule(opts));
        }

        doc.loadZip(zip);
        // 这里是设置导出的数据
        if (extraData === null) {
            doc.setData({
                // tableData:_this.tableData, //基础信息
                // roleData: _this.tableData.roleData
                [dataName] : data
                // arr: _this.arr // 评语
            })
        }
        else {
            doc.setData({
                // tableData:_this.tableData, //基础信息
                // roleData: _this.tableData.roleData
                [dataName] : data,
                [extraDataName] : extraData
                // arr: _this.arr // 评语
            })
        }
        try {
            doc.render()
        } catch (error) {
            var e = {
                message: error.message,
                name: error.name,
                stack: error.stack,
                properties: error.properties,
            }
            console.log(JSON.stringify({
                error: e
            }));
            throw error;
        }
        const out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        })
        saveAs(out, `${name}.docx`)
    })
}
